clc;
clear all;
close all;


%% INITIALIZATION
m = 8;
n = 8;
r = 8;
k = 1;
q = 0.8;
lower = 0;
upper = 0.9999;
t = 0;
z = 0;
filename = '4442.txt';
out_fid = fopen(filename, 'w');
a = rnd_near(lower,m);
b = rnd_near(upper,n);

%% SET UP SIMULATION
step = 2^(-(n+k));
ulp = 2^(-n);
f = 0:step:step*10;
e_star_f = zeros(size(f));

for i=1:length(f)
    max_err = get_max_err(lower, upper, m, n, r, k, f(i));
    e_star_f(i) = max_err;
end
% e_star_f = e_star_f/ulp;
e_fo = ones(size(f));
cor_const = ccm_const(m, n, r, k);
max_err_const = get_max_err(lower, upper, m, n, r, k, cor_const);
% max_err_const = max_err_const/ulp;
e_fo = e_fo * max_err_const;
plot(f/step, e_star_f);hold on;
plot(f/step, e_fo);
set(gca,'YTick',0:0.0625:1);

%% Compare f* vs f0 with some distribution
step_a = 2^(-m);
step_b = 2^(-n);
N = (floor((upper-lower)/step_a) + 1);
f_star = 2 * step;
f_0 = cor_const;

num_sim = 20;
E_sim_fstar = zeros(1, num_sim);
E_sim_fo = zeros(1, num_sim);
E_sim_vct = zeros(1, num_sim);
E_sim_hct = zeros(1, num_sim);
for i=1:num_sim
    if i>0 
        rng('shuffle');
        p = rand(1, N);
        p = p/sum(p);
    else
        p = ones(1, N) * 1.0/2^N;
    end
    E_sim_fstar(i) = get_ev(lower, upper, m, n, r, k, f_star, p);
    E_sim_fo(i) = get_ev(lower, upper, m, n, r, k, f_0, p);
    [E_sim_vct(i), E_sim_hct(i)] = get_ev_vhct(lower, upper, m, n, r, k, p);
end
figure;
plot(E_sim_fstar, 'b--o'); hold on; plot(E_sim_fo, 'c--*');
hold on; plot(E_sim_vct, 'r--+');
hold on; plot(E_sim_hct, 'g--');
legend('minimax correction','constant correction', 'VCT', 'HCT');

%% Compare f*, fo, vct, hct

clc;
clear all;
close all;

m = 4;
n = 4;
r = 4;
lower = 0;
upper = 0.99999999;
a = rnd_near(lower,m);
b = rnd_near(upper,n);

h = 0:6;
mm_ulp = ones(size(h)) * 2^(-m);
mm_f_star = zeros(size(h));
mm_f_o = zeros(size(h));
mm_vct = zeros(size(h));
mm_hct = zeros(size(h));
for i=1:length(h)
    [mm_f_star(i),mm_f_o(i), mm_vct(i), mm_hct(i)] = get_max_err_all(lower, upper, m, n, r, h(i));
end

figure;
plot(h, mm_f_star, 'b--o'); hold on;
plot(h, mm_f_o, 'c--*');hold on;
plot(h, mm_vct, 'r--+');hold on;
plot(h, mm_hct, 'g--'); hold on;
legend('minimax correction','constant correction', 'VCT', 'HCT');

